import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ljy_flutter/get/controller_example/getx_home_controller.dart';

class GetxControllerExampleApp extends StatelessWidget {
  const GetxControllerExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'GetX Controller Example',
      home: GetxControllerWidget(),
    );
  }

}

class GetxControllerWidget extends StatelessWidget {
  GetxControllerWidget({super.key});

  GetxHomeController getxHomeController = Get.put(GetxHomeController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('GetX Controller Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text('This is a placeholder for GetX Controller Example'),
            // 第一种
            Obx(() => Text('老师名称：${getxHomeController.teacher.name.value} age: ${getxHomeController.teacher.age.value}')),

            // 第二种
            GetX<GetxHomeController>(
              init: GetxHomeController(),
              builder: (controller) {
              return Text('老师名称：${controller.teacher.name} age: ${controller.teacher.age}');
            }),

            GetBuilder<GetxHomeController>(init: getxHomeController, builder: (controller) {
              return Text('老师名称111：${controller.teacher.name} age: ${controller.teacher.age}');
            }),

            SizedBox(height: 20,),
            ElevatedButton(onPressed: (){
              getxHomeController.conertToUpperCase();
            }, child: Text('转换为大写')),

            SizedBox(height: 20,),
            ElevatedButton(onPressed: (){
              getxHomeController.conertToUpperCase2();
            }, child: Text('转换为大写2')),

            SizedBox(height: 20,),
            GetX<GetxHomeController>(
              init: GetxHomeController(),
              builder: (controller) {
              return Text('学生名称：${controller.student.value.name} age: ${controller.student.value.age}');
            }),

            SizedBox(height: 20,),
            ElevatedButton(onPressed: (){
              getxHomeController.changeStudentName();

              //Get.find<GetxHomeController>().conertToUpperCase();

            }, child: Text('转换学生大写2')),

            SizedBox(height: 20,),
            Obx(() => Text('count: ${getxHomeController.count.value}', style: TextStyle(fontSize: 20, color: Colors.red),)),
            SizedBox(height: 20,),
            ElevatedButton(onPressed: (){
              getxHomeController.increment();
            }, child: Text('计数器加加'),)
          ],
        ),
      ),
    );
  }
}